//工具箱

const os = require("os");
const fs = require("fs");

APP.tools = {};
let tool = APP.tools;


const $chars = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
const $charsMap = (() => {
    let map = [];
    for (let i = 0, len = $chars.length; i < len; i++) {
        map[$chars.charAt(i)] = i;
    }
    return map;
})();

/**
 * 随机字符串
 * @param len 长度
 * @return 字符串
 * */
APP.tools.randomString = (len) => {
    len = len || 1024;
    var maxPos = $chars.length;
    var str = "";
    for (let i = 0; i < len; i++) {
        str += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return str;
};

APP.tools.radixtoMax = (int) => {
    if (!(int instanceof BigInt)) int = BigInt(int);
    let sBuilder = [];
    let radix = BigInt($chars.length);
    do {
        let remainder = int % radix;
        sBuilder.push($chars.charAt(Number.parseInt(remainder)));
        int = int / radix;
    } while (int !== 0n);

    return sBuilder.reverse().join("");
}
APP.tools.radixTo10 = (str) => {
    if (!APP.tools.radixInMax(str)) return NaN;
    let sum = BigInt(0);
    let radix = $chars.length;
    for (let i = 0, len = str.length; i < len; i++) {
        sum += BigInt($charsMap[str.charAt(len - i - 1)] * Math.pow(radix, i));
    }
    return sum.toString();
}
APP.tools.radixInMax = (str) => {
    if (!str) return false;
    for (let i = 0, len = str.length; i < len; i++) {
        if ($charsMap[str.charAt(i)] === undefined) return false;
    }
    return true;
}

APP.tools.mCopyFileSync = (oldpath, newpath) => {
    let resetData = fs.readFileSync(oldpath, {
        encoding: "UTF-8"
    });
    fs.writeFileSync(newpath, resetData, {
        encoding: "UTF-8"
    });
    return true;
};
